package webservices;
import java.sql.Date;

import javax.jws.WebService;
import beans.Customer;
import beans.CustomerLogin;
import beans.Customer_Rentals;
import beans.Movie;
import dbPool.ConnectionDAO;
import dao.CustomerDAO;
import dao.CustomerLoginDAO;
import dao.Customer_RentalsDAO;
import dao.MovieDAO;

@WebService
public class customer {
	
	public int signUp(String C_Id, String C_First_Name, String C_Last_Name, String C_Email, String C_Address, String C_City, String C_State, String C_Zipcode, int C_Premium, String C_Username, String C_Password, String C_Security_Question, String C_Security_Answer ){
		try{
			Customer cBean = new Customer(C_Id, C_First_Name, C_Last_Name, C_Email, C_Address, C_City, C_State, C_Zipcode, C_Premium);
			CustomerDAO cDAO = new CustomerDAO();
			
			CustomerLogin cLoginBean = new CustomerLogin(C_Id, C_Username, C_Password, C_Security_Question, C_Security_Answer);
			CustomerLoginDAO clDAO = new CustomerLoginDAO();
			
			int result = cDAO.createCustomer(cBean);
			int result1 = clDAO.createCustomerLogin(cLoginBean);
			
			if(result==0&&result==0){
				return 0;
			}
			else{
				return 1;
			}
			
		}
		catch(Exception e){
			e.printStackTrace();
			return 1;
		}
	}
	
	public String signIn(String username, String password){
		try{
			CustomerLoginDAO clDAO = new CustomerLoginDAO();
			String result = clDAO.authenticateCustomer(username, password);
			return result;
		}
		catch(Exception e){
			e.printStackTrace();
			return "error";
		}
	}
	
	public Customer getProfile(String C_Id){
		Customer cBean = null;
		try {
			CustomerDAO cdao = new CustomerDAO();
			cBean = cdao.retrieveCustomer(C_Id, "*");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cBean;
	}
	
	public Customer[] getAllCustomers(){
		Customer[] cBean = null;
		try {
			CustomerDAO cdao = new CustomerDAO();
			cBean = cdao.retrieveAllCustomers();
			return cBean;
		}
		catch(Exception e){
			e.printStackTrace();
			return cBean;
		}
	}
	
	public Customer[] searchCustomers(String key, String value){
		Customer[] cBean = null;
		try {
			CustomerDAO cdao = new CustomerDAO();
			cBean = cdao.retrieveAllCustomers(key, value);
			return cBean;
		}
		catch(Exception e){
			e.printStackTrace();
			return cBean;
		}
	}
	
	public int updateCustomerInfo(String C_Id, String key, String value){
		try{
			CustomerDAO cdao = new CustomerDAO();
			int result = cdao.updateCustomer(C_Id, key, value);
			return result;
		}
		catch(Exception e){
			e.printStackTrace();
			return 1;
		}
	}
	
	public int deleteCustomer(String C_Id){
		try{
			CustomerDAO cdao = new CustomerDAO();
			int result = cdao.deleteCustomer(C_Id);
			return result;
		}
		catch(Exception e){
			e.printStackTrace();
			return 1;
		}
	}
	
	public int createMovie(String M_Id, String M_Title, String M_Banner, String M_Release_Year, String M_Rental_Price, String M_Category, int M_Available_Copies, String M_Description ){
		try{
			MovieDAO mdao = new MovieDAO();
			Movie cBean = new Movie();
			
			cBean.setM_Available_Copies(M_Available_Copies);
			cBean.setM_Banner(M_Banner);
			cBean.setM_Category(M_Category);
			cBean.setM_Description(M_Description);
			cBean.setM_Id(M_Id);
			cBean.setM_Release_Year(M_Release_Year);
			cBean.setM_Rental_Price(M_Rental_Price);
			cBean.setM_Title(M_Title);
			
			
			int result = mdao.createMovie(cBean);
			return result;
		}
		catch(Exception e){
			e.printStackTrace();
			return 1;
		}
	}
	
	public Movie retrieveMovie(String M_Id, String Column_Name){
		
		MovieDAO mdao = new MovieDAO();
		Movie cBean = new Movie();
		
		try{
			cBean = mdao.retrieveMovie(M_Id, Column_Name);
			return cBean;
		}
		catch(Exception e){
			e.printStackTrace();
			return cBean;
		}
		
	}
	
	public Movie[] retrieveAllMovies(){
		
		Movie[] cBean = null;
		MovieDAO mdao = new MovieDAO();
		
		try{
			cBean = mdao.retrieveAllMovies();
			return cBean;
		}
		catch(Exception e){
			e.printStackTrace();
			return cBean;
		}
	}
	
	public Movie[] searchMovies(String key, String value){
		
		Movie[] cBean = null;
		MovieDAO mdao = new MovieDAO();
		
		try{
			cBean = mdao.searchMovies(key, value);
			return cBean;
		}
		catch(Exception e){
			e.printStackTrace();
			return cBean;
		}
	}
	
	public int updateMovie(String M_Id, String key, String value){
		try{
			MovieDAO mdao = new MovieDAO();
			int result = mdao.updateMovie(M_Id, key, value);
			return result;
		}
		catch(Exception e){
			e.printStackTrace();
			return 1;
		}
	}
	
	
	public int deleteMovie(String M_Id){
		try{
			MovieDAO mdao = new MovieDAO();
			int result = mdao.deleteMovie(M_Id);
			return result;
		}
		catch(Exception e){
			e.printStackTrace();
			return 1;
		}
	}
	
	public int issueMovie(String C_Id, String M_Id, String CR_Date_Out, String CR_Date_Returned, String CR_Rental_Id){
		try{
			Customer_RentalsDAO crdao = new Customer_RentalsDAO();
			Customer_Rentals cBean = new Customer_Rentals();
			
			cBean.setC_Id(C_Id);
			cBean.setM_Id(M_Id);
			cBean.setCR_Date_Out(Date.valueOf(CR_Date_Out));
			cBean.setCR_Date_Returned(Date.valueOf(CR_Date_Returned));
			cBean.setCR_Rental_Id(CR_Rental_Id);
			
			int result=crdao.createCustomer_Rentals(cBean);
			
			return result;
		}
		catch(Exception e){
			e.printStackTrace();
			return 1;
		}
	}
	
	public int returnMovie(String CR_Rental_Id){
		try{
			Customer_RentalsDAO crdao = new Customer_RentalsDAO();
			
			int result=crdao.deleteCustomer_Rentals(CR_Rental_Id);
			
			return result;
		}
		catch(Exception e){
			e.printStackTrace();
			return 1;
		}
	}
	
	public Customer_Rentals dummy(){
		Customer_Rentals cr = new Customer_Rentals();
		return cr;
		
	}
	

}
